package com.example.teatea.controller;

import com.example.teatea.dao.AddMember;
import com.example.teatea.dao.CheckMember;
import com.example.teatea.project.Member;
import com.example.teatea.tools.GetTime;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.stage.Stage;

import javax.swing.*;
import java.net.URL;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ResourceBundle;

public class AddMemberController implements Initializable {
    public TextField name;
    public TextField phone;
    public Button sureBt;
    public DatePicker birth;
    public RadioButton man;
    public ToggleGroup sex;
    public RadioButton woman;
    public Button cancleBt;

    public void toCancle(ActionEvent actionEvent) {
        Stage stage = (Stage) cancleBt.getScene().getWindow();
        stage.close();
    }

    public void toSure(ActionEvent actionEvent) {
        Member member = new Member();
        if(name.getText().equals("")){
            JOptionPane.showMessageDialog(null, "手机号不能为空！", "提示", JOptionPane.ERROR_MESSAGE);
        }else if(phone.getText().equals("")){
            JOptionPane.showMessageDialog(null, "手机号不能为空！", "提示", JOptionPane.ERROR_MESSAGE);
        }else{
            member.setmName(name.getText());
            member.setmPhone(phone.getText());
            member.setmSex(sex.getSelectedToggle().getUserData().toString());
            member.setmTime(new GetTime().getTime());
            member.setmBirthday(birth.getValue().toString());
            int r = new AddMember().addMember(member);
            if(r > 0){
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setHeaderText("数据添加成功！");
                alert.show();
                Stage stage = (Stage) sureBt.getScene().getWindow();
                stage.close();
                MainController mainController = (MainController) ControllerManager.controllerMap.get("main");
                mainController.loadMemberDate();
            }
        }
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        //设置日期选择框的值
        String date = "2003-08-29-06:06:06";
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH:mm:ss");
        LocalDate date2 = LocalDate.parse(date,fmt);
        birth.setValue(date2);
        //手机号输入框失去焦点事件
        phone.focusedProperty().addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> observableValue, Boolean aBoolean, Boolean t1) {
                //数据改变
                if(aBoolean && !t1){
                    //数据库查询会员手机号是否存在
                    if(new CheckMember().checkMember(phone.getText())){
                        Alert alert=new Alert(Alert.AlertType.INFORMATION);
                        alert.setHeaderText("该会员已存在，请重新输入！");
                        alert.show();
                        phone.requestFocus();
                    }
                }
            }
        });
    }
}
